﻿using System.Drawing;
using System.Windows.Forms;
using monitor_2_DBwork;

namespace monitor_server_2
{
    public partial class AddRegion : Form
    {
        private readonly TreeNode _node;
        private DeviceList.RefreshTree d;

        public AddRegion(TreeNode sender1, DeviceList.RefreshTree sender2)
        {
            InitializeComponent();
            _node = sender1;
            d = sender2;
        }

        private void AddRegion_Load(object sender, System.EventArgs e)
        {
            Text = Main.Rm.GetString("AddRegion");
            coordsImageBox.Image = (Image)Main.Rm.GetObject("compass_blue_64");
            acceptButton.Text = Main.Rm.GetString("Accept");
            acceptButton.Image = (Image)Main.Rm.GetObject("accept_image_16");
            cancelButton.Text = Main.Rm.GetString("Cancel");
            cancelButton.Image = (Image) Main.Rm.GetObject("cancel_image_16");
            stationLabel.Text = Main.Rm.GetString("Station");
            regionLabel.Text = Main.Rm.GetString("Region");
            blrLabel.Text = Main.Rm.GetString("BLR");

            regionBox.DropDownStyle = ComboBoxStyle.DropDownList;
            blrBox.DropDownStyle = ComboBoxStyle.DropDownList;

            coordinatesSetter1.SetDestinations("N");
            coordinatesSetter1.DestText = "N";
            coordinatesSetter1.MinDegrees = 23;
            coordinatesSetter1.MaxDegrees = 32;

            coordinatesSetter2.SetDestinations("E");
            coordinatesSetter2.DestText = "E";
            coordinatesSetter2.MinDegrees = 51;
            coordinatesSetter2.MaxDegrees = 56;

            GetRegions();
            GetBlrs();

            var options = (DeviceTreeNodeOptions) _node.Tag;
            regionBox.SelectedItem = _node.Text;



        }

        private void AddRegion_Shown(object sender, System.EventArgs e)
        {
            Icon = (Icon)Main.Rm.GetObject("antenna_16_ico");
        }

        void GetRegions()
        {
            var regions = DBwork.GetRegionList();
            regionBox.Items.AddRange(regions.ToArray());
        }

        void GetBlrs()
        {
            var blrs = DBwork.GetBlrList();
            blrBox.Items.AddRange(blrs.ToArray());
        }

        private void cancelButton_Click(object sender, System.EventArgs e)
        {
            Close();
        }

        private void acceptButton_Click(object sender, System.EventArgs e)
        {
            if (stationBox.Text.Length < 1)
            {
                MessageBox.Show(Main.Rm.GetString("WrongParameter") + Main.Rm.GetString("Station"));
                return;
            }
            if (regionBox.SelectedIndex < 0)
            {
                MessageBox.Show(Main.Rm.GetString("WrongParameter") + Main.Rm.GetString("Region"));
                return;
            }
            if (blrBox.SelectedIndex < 0)
            {
                MessageBox.Show(Main.Rm.GetString("WrongParameter") + Main.Rm.GetString("BLR"));
                return;
            }

            if (DBwork.AddStation(
                stationBox.Text, 
                (string) regionBox.SelectedItem, 
                (string) blrBox.SelectedItem, 
                coordinatesSetter1.Coordinates + coordinatesSetter2.Coordinates))
            {
                MessageBox.Show(Main.Rm.GetString("Complete"));
                d();
                Close();
            }
            else
            {
                MessageBox.Show(Main.Rm.GetString("Error"));
            }

        }
    }
}
